﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using sixgod.Base.Controllers;
using sixgod.Dtos.System.Search;
using sixgod.Model.System.Menu;
using sixgod.Service.Dictionary;

namespace sixgod.Module.SystemCore.Controllers.System
{
    [Route("[Controller]"),AllowAnonymous]
    public class MenuController:BaseController
    {
        private IMenuService _service;
        private IMapper _mapper;

        public MenuController(IMenuService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        #region 查询

        [HttpGet("{Id}")]
        public async Task<IActionResult> Get(string Id)
        {
            var model = await _service.GetByIdAsync(Id);
            return Success(model);
        }

        [HttpGet("UserMenu")]
        public async Task<IActionResult> GetUserMenu()
        {
            var loginUid = "0";
            var result = await _service.GetUserMenu(loginUid);
            return Success(result);
        }
        
        [HttpGet("AllList")]
        public async Task<IActionResult> GetAllMenuList()
        {
            var list = await _service.GetAllListForSelect();
            return Success(list);
        }


        [HttpGet("page")]
        public async Task<IActionResult> GetPageList([FromQuery]MenuPageSearch page)
        {
            var result = await _service.GetPageList(page);
            return Success(result);
        }

        [HttpGet("getChildrenMenu")]
        public async Task<IActionResult> GetChildrenMenu(string code)
        {
            var result = await _service.GetChildrenMenu(code);
            return Success(result);
        }

        #endregion

        #region CUD

        [HttpPost]
        public async Task<IActionResult> AddMenu([FromBody]SystemMenu model)
        {
            var res = await _service.Save(model);
            if (res)
            {
                return Success("新增成功");
            }
            return Fail("新增失败");
        }

        [HttpPut]
        public async Task<IActionResult> UpdateMenu([FromBody]SystemMenu model)
        {
            var res = await _service.Save(model);
            if (res)
            {
                return Success("修改成功");
            }
            return Fail("修改失败");
        }

        [HttpDelete("{Id}")]
        public async Task<IActionResult> Delete(string Id)
        {
            var res = await _service.Delete(Id);
            if (res>0)
            {
                return Success("删除成功");
            }
            return Fail("删除失败");
        }

        #endregion

    }
}
